package lk.icta.common.dao.impl;

import lk.icta.common.AppConstants;
import lk.icta.common.ErrorCodes;
import lk.icta.common.dao.RailwayStationDAO;
import lk.icta.common.entity.RailwayStation;
import lk.icta.user.entity.User;

import javax.persistence.Query;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Supun Viraj Rathnayaka
 */
public class RailwayStationDAOImpl extends BaseDAO implements RailwayStationDAO {

    private final Map<Integer, String> siNames = new HashMap<Integer, String>();
    private final Map<Integer, String> enNames = new HashMap<Integer, String>();
    private final Map<Integer, String> taNames = new HashMap<Integer, String>();

    @Override
    public Map<Integer, String> getAllRailwayStationsByUserLanguage(String language, User user) {
        logger.debug("Listing railway stations in {} by {}", language, user.getUsername());
        Query q = em.createNamedQuery("getAllRailwayStationsByUserLanguage");
        List<RailwayStation> railwayStationList = q.getResultList();
        for (RailwayStation railwayStation : railwayStationList) {
            siNames.put(railwayStation.getId(), railwayStation.getStationSiName());
            enNames.put(railwayStation.getId(), railwayStation.getStationEnName());
            taNames.put(railwayStation.getId(), railwayStation.getStationTaName());
        }
        if (AppConstants.SINHALA.equals(language)) {
            return siNames;
        } else if (AppConstants.ENGLISH.equals(language)) {
            return enNames;
        } else if (AppConstants.TAMIL.equals(language)) {
            return taNames;
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }
        return Collections.emptyMap();
    }

    @Override
    public Map<Integer, String> getAllIPWZoneNamesByIPWId(int ipwId, String language, User user) {
        Query q = em.createNamedQuery("getAllRailwayStationsByIpw");
        q.setParameter("ipwId", ipwId);
        List<RailwayStation> railwayStationList = q.getResultList();
        siNames.clear();
        enNames.clear();
        taNames.clear();
        for (RailwayStation railwayStation : railwayStationList) {
            siNames.put(railwayStation.getId(), railwayStation.getStationSiName());
            enNames.put(railwayStation.getId(), railwayStation.getStationEnName());
            taNames.put(railwayStation.getId(), railwayStation.getStationTaName());
        }
        if (AppConstants.SINHALA.equals(language)) {
            return siNames;
        } else if (AppConstants.ENGLISH.equals(language)) {
            return enNames;
        } else if (AppConstants.TAMIL.equals(language)) {
            return taNames;
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }
        return Collections.emptyMap();
    }

    @Override
    public RailwayStation getRailwayStationByCode(String stationCode) {
        logger.debug("Loading Station {}", stationCode);
        Query q = em.createNamedQuery("getRailwayStationByCode");
        q.setParameter("code", stationCode);
        List<RailwayStation> s = q.getResultList();
        return (s.size()>0) ? s.get(0) : null;
    }
}
